import groupSqlite from '@/sqlite_services/group.js'
import groupMembersSqlite from '@/sqlite_services/group_members.js'
import $store from '@/store/index.js';
import msgSqlite from "@/sqlite_services/msg.js"
export default {
	/* 获取群信息 */
	async getGroupInfo(group_id) {
		try{
			let res = await uni.$api.groups.infoConfig({group_id:group_id})
			if(res){
				let sqlData = {
					"group_id": res.data.group_id,
					"g_no": res.data.g_no,
					"creator_id": res.data.creator_id,
					"avatar_url": res.data.avatar_url,
					"group_name": res.data.group_name,
					"description": res.data.description,
					"notice": res.data.notice,
					"create_time": res.data.create_time,
					"update_time": res.data.update_time,
					"is_review": res.data.is_review,
					"is_invitation": res.data.is_invitation,
					"is_muted": res.data.is_muted,
					"default_qun": res.data.default_qun,
					"is_vip": res.data.is_vip,
					"qun_level": res.data.qun_level,
					"show_sys_msg": res.data.show_sys_msg,
					"show_online_number": res.data.show_online_number,
					"show_ip": res.data.show_ip,
					"show_history": res.data.show_history,
					"show_qun_number": res.data.show_qun_number,
					"can_delete_quit_msg": res.data.can_delete_quit_msg,
					"can_withdraw_self_msg": res.data.can_withdraw_self_msg,
					"can_withdraw_other_msg": res.data.can_withdraw_other_msg,
					"can_send_images": res.data.can_send_images,
					"can_send_video": res.data.can_send_video,
					"can_send_audio": res.data.can_send_audio,
					"can_send_card": res.data.can_send_card,
					"can_quit_range": res.data.can_quit_range,
					"can_add_friends": res.data.can_add_friends,
					"can_edit_msg":res.data.can_edit_msg,
					"can_send_msg": res.data.can_send_msg,
					"tags":res.data.tags,
					"tags_font_color":res.data.tags_font_color,
					"show_member_list": res.data.show_member_list
				}
				console.log('-------',res.data.tags);
				//获取我在群内的身份一块搞上去
				let resRole = await uni.$api.groups.getRole({group_ids:[group_id]})
				if(resRole){
					sqlData.mygroup = '1'
					sqlData.userRole = resRole.data[group_id].role
					sqlData.userMuted = resRole.data[group_id].is_muted
					sqlData.userNickname = resRole.data[group_id].group_nickname || ''
				}else{
					sqlData.mygroup = '0'
				}
				
				
				
				//存储到数据库
				await groupSqlite.addOrEditGroupInfo(sqlData)
				//存储本次打开缓存过的群id,受socket连接的影响
				$store.commit('setOpenGroups',{group_id,type:'info'})
				//更新正在聊天中的信息
				$store.commit('updateChatInfo', { uuid:group_id, data: {
					nickname: sqlData.group_name,
					avatar_url: sqlData.avatar_url,
				}});
				//更新会话列表
				$store.dispatch('updateSession',{
					chat_id:group_id, 
					data:{
						chat_name:sqlData.group_name,
						nick_name:sqlData.group_name,
						chat_face:sqlData.avatar_url,
						chat_member_type:'',
						tags:sqlData.tags,
						tags_font_color:sqlData.tags_font_color, 
					}
				})
				return sqlData
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	/* 修改群信息 */
	async editGroupInfo(data,isRenewal) {
		try{
			let res = await uni.$api.groups.editInfo(data)
			if(res){
				//更新群资料
				groupSqlite.editGroupInfo(data.group_id,data)
				$store.commit('updateChatGroupInfo',data)
				//是否更新会话等信息
				if(isRenewal){
					//更新正在聊天中的信息
					$store.commit('updateChatInfo', { uuid:data.group_id, data: {
						nickname: data.group_name,
						avatar_url: data.avatar_url,
					}});
					//更新会话列表
					$store.dispatch('updateSession',{
						chat_id:data.group_id, 
						data:{
							chat_name:data.group_name,
							nick_name:data.group_name,
							chat_face:data.avatar_url,
							chat_member_type:''
						}
					})
				}
				return res
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	/* 修改群配置 */
	async editGroupConf(data) {
		try{
			let res = await uni.$api.groups.editConf(data)
			if(res){
				//更新群资料
				groupSqlite.editGroupInfo(data.group_id,data)
				
				$store.commit('updateChatGroupInfo',data)
				
				return res
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	/* 退群 */
	async memberOut(group_id) {
		try{
			let res = await uni.$api.groups.memberOut({group_id})
			if(res){
				return res
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	/* 解散群 */
	async dissolve(group_id) {
		try{
			let res = await uni.$api.groups.dissolve({group_id})
			if(res){
				return res
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	/* 解散群 */
	async outOrDissolve(group_id) {
		try{
			const sqlres = await groupSqlite.getGroupInfo(group_id);
			console.log(sqlres);
			if (sqlres) {
				//现在更改需求，需要删除会话
				// 1.删除群资料，删除群成员，删除群消息，删除群会话
				//删除群信息   
				groupSqlite.delGroupInfo(group_id)
				//删除请求记录
				$store.commit('delOpenGroups', group_id)
				//删除群成员
				groupMembersSqlite.delGroupMembersAll(group_id)
				//删除会话
				$store.dispatch('removeSession', group_id)
				msgSqlite.delMsgUserId(group_id, 'group')
				const activeChatUuid = $store.state.chat.chatInfo && $store.state.chat.chatInfo.uuid || ''; // 访问根级状态的chatInfo
				console.log(activeChatUuid == group_id);
				if(activeChatUuid == group_id){
					uni.switchTab({
						url: '/pages/index/index'
					})
				}
			}
		}catch(e){
			//TODO handle the exception
		}
		// //删除群,软删除，修改状态而已 -- 保留会话的写法
		// groupSqlite.editGroupInfo(group_id,{
		// 	mygroup:0,
		// 	userRole:'member',//身份返璞归真
		// })
		// $store.commit('updateChatGroupInfo',{
		// 	group_id,
		// 	mygroup:0,
		// 	userRole:'member',
		// })
		// //另外还需要删除所有群成员的列表
		// // groupMembersSqlite.delGroupMembersAll(group_id)
		
		// $store.commit('delOpenGroups',group_id)
	},
}